using System.Runtime.CompilerServices;
using System.Text;
using MySql.Data.MySqlClient;

namespace ERP;

public class BC_Users
{
    public static Object Login(string UserName, string Password)
    {
        // 写查询用户信息的SQL
        StringBuilder sqlB = new StringBuilder();
        sqlB.Length = 0;
        sqlB.AppendLine("SELECT ");
        sqlB.AppendLine("COUNT(users.UserID) AS Count ");
        sqlB.AppendLine("FROM `users` ");
        sqlB.AppendLine($"WHERE users.UserName = '{UserName}' ");
        sqlB.AppendLine($"AND users.EncryptedPassword = '{Password}' ");
        sqlB.AppendLine("; ");

        // 运行SQL获取查询结果
        int Count = Convert.ToInt32(BC_MySqlUtils.ExecuteSQLGetScalar(sqlB.ToString()));

        // 判断是否有记录， 即是否登录成功
        bool IsLogin = false;
        string Message = "";
        if (Count > 0)
        {
            IsLogin = true;
            Message = "登录成功！";
        }
        else
        {
            IsLogin = false;
            Message = "登录失败！用户名或密码错误！";
        }

        // 返回登录结果
        return new
        {
            LoginStatus = IsLogin,
            Message
        };
    }
}